package version1;

import javax.imageio.ImageIO;
import javax.swing.*;        
import javax.swing.plaf.basic.BasicTabbedPaneUI.TabbedPaneLayout;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.xml.ws.handler.MessageContext.Scope;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;

public class GUI extends JFrame implements ActionListener {
	// To define the two cards
	final static String CORETABLE = "CoreTable";
	final static String JOBTABLE = "JobTable";
	
	private JButton coreButton = new JButton("List cores");
	private JButton submitButton = new JButton("Submit job");
	private JButton filtreringButton = new JButton("Filtering");
	private JButton closeButton = new JButton("Close Program");
	
	private JButton SwitchToCorePanelButton = new JButton("Switch to cores");
	private JButton SwitchToJobsPanelButton = new JButton("Switch to jobs");
	
	private JPanel mainPanel = new JPanel();
	private JPanel buttonPanel = new JPanel();
	private JPanel topPanel = new JPanel();
	private JPanel Scrollpanel = new JPanel();
	private JPanel corePanel = new JPanel();
	
	private JPanel jobPanel = new JPanel();
	private JPanel cards;
	
	private CardLayout cardlayout = new CardLayout();
		
	private CoreTable coreModel;
	private CoreTable jobModel;
	
    public GUI(){
    	
    	//Cards
    	cards = new JPanel(cardlayout);
    	cards.add(corePanel, CORETABLE);
    	cards.add(jobPanel, JOBTABLE);
//TODO make a tab button icon
//    	try {
//    	    Image img = ImageIO.read(getClass().getResource("file here"));
//    	    SwitchToCorePanelButton.setIcon(new ImageIcon(img));
//    	    SwitchToJobsPanelButton.setIcon(new ImageIcon(img));
//    	  } catch (IOException ex) {
//    		  ex.printStackTrace();
//    	  }
    	//Layout
        mainPanel.setLayout(new BorderLayout());
        
        //Background
        buttonPanel.setBackground(Color.black);
        topPanel.setBackground(Color.black);
        Scrollpanel.setBackground(Color.black);
        
        
        //Add to Panel
        mainPanel.add(cards, BorderLayout.CENTER);
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        mainPanel.add(topPanel, BorderLayout.NORTH);

        
        //Table for CoreTable
        coreModel = new CoreTable("CORE");
    	JTable coreTable = new JTable(coreModel);
        coreTable.setPreferredScrollableViewportSize(new Dimension(800, 600));
        coreTable.setFillsViewportHeight(true);
        coreTable.setEnabled(false);
        coreTable.setAutoCreateRowSorter(true);

        
        //ScrollTable for CoreTable
        Scrollpanel.setLayout(new FlowLayout());
    	JScrollPane scrollPane = new JScrollPane(coreTable,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,    		    
    	JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    	coreTable.setFillsViewportHeight(true);
    	coreTable.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
    	corePanel.add(scrollPane, BorderLayout.CENTER);
    	scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        //Table for jobTable
        jobModel = new CoreTable("JOB");
    	JTable jobTable = new JTable(jobModel);
    	jobTable.setPreferredScrollableViewportSize(new Dimension(800, 600));
    	jobTable.setFillsViewportHeight(true);
    	jobTable.setEnabled(false);
    	jobTable.setAutoCreateRowSorter(true);

        
        //ScrollTable for jobTable
        Scrollpanel.setLayout(new FlowLayout());
    	JScrollPane scrollPane2 = new JScrollPane(jobTable,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,    		    
    	JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    	jobTable.setFillsViewportHeight(true);
    	jobTable.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
    	jobPanel.add(scrollPane2, BorderLayout.CENTER);
    	scrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        scrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        

        //Add to Frame
        add(mainPanel);
        buttonPanel.add(coreButton);
        buttonPanel.add(submitButton);        
        buttonPanel.add(filtreringButton);
        buttonPanel.add(closeButton);
        topPanel.add(SwitchToCorePanelButton);
        topPanel.add(SwitchToJobsPanelButton);
        
        //ActionListener
        coreButton.addActionListener(this);
        submitButton.addActionListener(this);
        filtreringButton.addActionListener(this);
        closeButton.addActionListener(this);
        SwitchToCorePanelButton.addActionListener(this);
        SwitchToJobsPanelButton.addActionListener(this);
        
        
        //Other options
        setTitle("HTCondor GUI");
        setVisible(true);
        pack();
        setDefaultLookAndFeelDecorated(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jobModel.run();
//        coreModel.run();
        

    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == coreButton){
//Remove method when Condor API is implemented
			coreModel.testAddMethod();
			jobModel.testAddMethod();
		}
		if(e.getSource() == submitButton){
			SubmitFile file = new SubmitFile();
		}
		if(e.getSource() == filtreringButton){
			FiltreringGUI filtering = new FiltreringGUI(coreModel); 
		}
		if(e.getSource() == closeButton){
			dispose();
		}
		if(e.getSource() == SwitchToCorePanelButton){
	           cardlayout.show(cards, CORETABLE);
		}
		if(e.getSource() == SwitchToJobsPanelButton){
	           cardlayout.show(cards, JOBTABLE);
		}
		
	}
    
}
